//
//  JRNaviBar.m
//  JR315
//
//  Created by linziyuan on 16/5/3.
//  Copyright © 2016年 creative. All rights reserved.
//

#import "JRNaviBar.h"
#import "ConstDefine.h"

@interface JRNaviBar ()

@property (nonatomic, strong) UIView *centerView;
@property (nonatomic, strong) NSArray *leftViews;
@property (nonatomic, strong) NSArray *rightViews;
@property (nonatomic, strong) UIImageView *bgView;

@property (nonatomic, assign) BOOL isPhone;
@property (nonatomic, assign) CGFloat height;
@end

@implementation JRNaviBar
{
    CGFloat _leftSpace;
    CGFloat _rightSpace;
    CGFloat _topSpace;
    CGFloat _leftPading;
    CGFloat _rightPading;
}

+ (instancetype)getJRNaviBarWithSuperView:(UIView *)superView centerView:(UIView *)centerView leftViews:(NSArray<UIView *> *)leftViews rightViews:(NSArray<UIView *> *)rightViews leftSpace:(CGFloat)leftSpace rightSpace:(CGFloat)rightSpace {
    if (!superView) {
        NSLog(@"superView 为空");
        return nil;
    }
    
    JRNaviBar *naviBar = [[JRNaviBar alloc] init];
    if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
        naviBar.isPhone = YES;
    }else {
        naviBar.isPhone = NO;
    }
    
    //    if (naviBar.isPhone) {
    //        naviBar.height = 42.5;
    //    }else {
    //        naviBar.height = 64;
    //    }
    naviBar.height = 56;
    naviBar.backgroundColor = [UIColor clearColor];
    naviBar.translatesAutoresizingMaskIntoConstraints = NO;
    [superView addSubview:naviBar];
    [naviBar lyt_alignLeftToParent];
    [naviBar lyt_alignRightToParent];
    [naviBar lyt_alignTopToParent];
    [naviBar lyt_setHeight:naviBar.height];
    [naviBar setUpBgView];
    if (centerView) {
        naviBar.centerView = centerView;
    }
    if (leftViews.count > 0) {
        naviBar.leftViews = leftViews;
    }
    if (rightViews.count > 0) {
        naviBar.rightViews = rightViews;
    }
    [naviBar setUpViewsWihtleftSpace:leftSpace rightSpace:rightSpace];
    return naviBar;
}

+ (instancetype)getJRNaviBarWithSuperView:(UIView *)superView centerView:(UIView *)centerView leftViews:(NSArray<UIView *> *)leftViews rightViews:(NSArray<UIView *> *)rightViews {
    CGFloat lSpace = 20.0;
    CGFloat rSpace = 20.0;
    if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
        lSpace = 10.0;
        rSpace = 10.0;
    }
    return [JRNaviBar getJRNaviBarWithSuperView:superView centerView:centerView leftViews:leftViews rightViews:rightViews leftSpace:lSpace rightSpace:rSpace];
}

- (void)setUpViewsWihtleftSpace:(CGFloat)leftSpace rightSpace:(CGFloat)rightSpace {
    
    _leftSpace = leftSpace;
    _rightSpace = rightSpace;
    _topSpace = 22.0;
    _leftPading = 6.0;
    _rightPading = 6.0;
    if (self.isPhone) {
        _leftPading = 4.0;
        _rightPading = 4.0;
    }
    CGFloat LOriginX = _leftSpace;
    CGFloat ROriginX = LOriginX;
    
    if (self.leftViews.count > 0) {
        
        for (NSInteger i = 0; i < _leftViews.count; i++) {
            UIView *lview = _leftViews[i];
            lview.translatesAutoresizingMaskIntoConstraints = NO;
            [self addSubview:lview];
            [lview lyt_setSize:CGSizeMake(lview.frame.size.width, lview.frame.size.height)];
            [lview lyt_alignLeftToParentWithMargin:LOriginX];
            if (self.isPhone) {
                CGFloat topOffset = (self.height - _topSpace - lview.frame.size.height) * 0.2;
                [lview lyt_alignTopToParentWithMargin:(_topSpace + topOffset)];
            }else {
              [lview lyt_centerYInParent];
            }
            LOriginX += lview.frame.size.width + _leftPading;
        }
    }
    
    if (self.rightViews.count > 0) {
        for (NSInteger i = 0; i < self.rightViews.count; i++) {
            UIView *rview = _rightViews[i];
            rview.translatesAutoresizingMaskIntoConstraints = NO;
            [self addSubview:rview];
            [rview lyt_setSize:CGSizeMake(rview.frame.size.width, rview.frame.size.height)];
            if (rview.tag == UNOFFSETTAG) {
                ROriginX = 0;
            }
            [rview lyt_alignRightToParentWithMargin:ROriginX];
            if (self.isPhone) {
                CGFloat topOffset = (self.height - _topSpace - rview.frame.size.height) * 0.2;
                [rview lyt_alignTopToParentWithMargin:(_topSpace + topOffset)];
            }else {
                [rview lyt_centerYInParent];
            }
            ROriginX += rview.frame.size.width + _rightPading;
        }
    }
    
    if (self.centerView) {
        _centerView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:_centerView];
        [_centerView lyt_setSize:CGSizeMake(_centerView.frame.size.width, _centerView.frame.size.height)];
        [_centerView lyt_centerXInParent];
        if (self.isPhone) {
            CGFloat topOffset = (self.height - _topSpace - _centerView.frame.size.height) * 0.2;
            [_centerView lyt_alignTopToParentWithMargin:(_topSpace + topOffset)];
        }else {
            [_centerView lyt_centerYInParent];
        }
    }
}

- (void)setUpBgView {
    self.bgView = [[UIImageView alloc] init];
    [self addSubview:self.bgView];
    [_bgView JRMAS_makeConstraints:^(JRMASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.bottom.equalTo(self).offset(1);
    }];
    UIImage *image = [UIImage imageNamed:@"bookshelf_purple_bar_bg.png"];
    _bgView.backgroundColor = [UIColor colorWithPatternImage:image];
    
}

- (void)updateLeftViews:(NSArray<UIView *> *)leftViews {
    if (self.leftViews) {
        for (UIView *leftV in self.leftViews) {
            [leftV removeFromSuperview];
        }
    }
    if (leftViews.count) {
        CGFloat LOriginX = _leftSpace;
        self.leftViews = leftViews;
        for (NSInteger i = 0; i < _leftViews.count; i++) {
            UIView *lview = _leftViews[i];
            lview.translatesAutoresizingMaskIntoConstraints = NO;
            [self addSubview:lview];
            [lview lyt_setSize:CGSizeMake(lview.frame.size.width, lview.frame.size.height)];
            [lview lyt_alignLeftToParentWithMargin:LOriginX];
            if (self.isPhone) {
                CGFloat topOffset = (self.height - _topSpace - lview.frame.size.height) * 0.2;
                [lview lyt_alignTopToParentWithMargin:(_topSpace + topOffset)];
            }else {
                [lview lyt_centerYInParent];
            }
            LOriginX += lview.frame.size.width + _leftPading;
        }
    }
}


- (void)updateRightViews:(NSArray<UIView *> *)rightViews {
    if (self.rightViews) {
        for (UIView *leftV in self.rightViews) {
            [leftV removeFromSuperview];
        }
    }
    if (rightViews.count) {
        self.rightViews = rightViews;
        CGFloat ROriginX = _leftSpace;
        for (NSInteger i = 0; i < self.rightViews.count; i++) {
            UIView *rview = _rightViews[i];
            rview.translatesAutoresizingMaskIntoConstraints = NO;
            [self addSubview:rview];
            [rview lyt_setSize:CGSizeMake(rview.frame.size.width, rview.frame.size.height)];
            if (rview.tag == UNOFFSETTAG) {
                ROriginX = 0;
            }
            [rview lyt_alignRightToParentWithMargin:ROriginX];
            if (self.isPhone) {
                CGFloat topOffset = (self.height - _topSpace - rview.frame.size.height) * 0.2;
                [rview lyt_alignTopToParentWithMargin:(_topSpace + topOffset)];
            }else {
                [rview lyt_centerYInParent];
            }
            ROriginX += rview.frame.size.width + _rightPading;
        }
    }
}

@end
